//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shp.Runtime.Contract;
using Shp.Runtime.DataModel.Sitka.Tests.Entities;

namespace Shp.Runtime.DataModel.Sitka.Tests
{
    /// <summary>
    /// Summary description for WorkflowDataModelFixture
    /// </summary>
    [TestClass]
    public class WorkflowDataModelFixture
    {
        private TenantEntity tenantMock = Mocks.MockHelper.GetTenant();
        private MetadataInitialize metadataInitializeData;
        private TestableRepositoryFactory factory;

        [TestInitialize]
        public void TestInitialize()
        {
            metadataInitializeData = new MetadataInitialize();

            factory = new TestableRepositoryFactory(metadataInitializeData.TenantId);
            factory.CreateContainers();

            metadataInitializeData.Container = factory.MetadataContainerName;
            metadataInitializeData.CreateTenant();
        }

        [TestCleanup]
        public void TestCleanUp()
        {
            factory.DeleteContainers();
        }

        [TestMethod]
        public void ShouldAddWorkflowDef()
        {
            WorkflowDto workflowMock = Mocks.MockHelper.GetWorkflowEntityMock();

            WorkflowDataModel datamodel = new WorkflowDataModel(factory);

            //insert workflowDef.
            Guid workflowId = datamodel.AddWorkflowDef(metadataInitializeData.TenantId, workflowMock.Name, workflowMock.Xoml);

            //delete workflowDef.
            datamodel.DeleteWorkflowDef(workflowId);
        }

        [TestMethod]
        public void ShouldGetWorkflowDef()
        {
            WorkflowDto workflowMock = Mocks.MockHelper.GetWorkflowEntityMock();

            WorkflowDataModel _datamodel = new WorkflowDataModel(factory);

            //insert workflowDef.
            metadataInitializeData.AddWorkflow(workflowMock.Id, workflowMock.Name, workflowMock.Xoml);

            //get workflowDef.
            WorkflowDto result = _datamodel.GetWorkflowDef(workflowMock.Id);

            //Assert
            Assert.AreEqual(workflowMock.Name, result.Name);
            Assert.AreEqual(workflowMock.Xoml, result.Xoml);

            metadataInitializeData.DeleteEntity(workflowMock.Id.ToString());
        }

        [TestMethod]
        public void ShouldGetWorkflowDefList()
        {
            WorkflowDataModel dataAccessModel = new WorkflowDataModel(factory);
            //add workflow definition 			
            Guid workflowDefMock1 = dataAccessModel.AddWorkflowDef(metadataInitializeData.TenantId, "WorkflowDefTest1", "Xoml2");
            Guid workflowDefMock2 = dataAccessModel.AddWorkflowDef(metadataInitializeData.TenantId, "WorkflowDefTest2", "Xoml2");

            //get workflow definition list.

            Dictionary<Guid, string> list = dataAccessModel.GetWorkflowDefList(metadataInitializeData.TenantId);

            Assert.IsNotNull(list);

            //remove workflow definition.
            dataAccessModel.DeleteWorkflowDef(workflowDefMock1);
            dataAccessModel.DeleteWorkflowDef(workflowDefMock2);

        }

        [TestMethod]
        public void ShouldInsertRuleSetWhenUpdateRuleSetDefFirstTime()
        {
            WorkflowDataModel dataAccessModel = new WorkflowDataModel(factory);
            dataAccessModel.UpdateRuleSetDef(metadataInitializeData.TenantId, "ruleSetXaml");

            string xaml = dataAccessModel.GetRuleSetDef(metadataInitializeData.TenantId);

            Assert.IsNotNull(xaml);

        }

        [TestMethod]
        public void ShouldUpdateRuleSetDef()
        {
            WorkflowDataModel dataAccessModel = new WorkflowDataModel(factory);
            dataAccessModel.UpdateRuleSetDef(metadataInitializeData.TenantId, "ruleSetXaml");
            dataAccessModel.UpdateRuleSetDef(metadataInitializeData.TenantId, "ruleSetXamlUpdated");

            string xaml = dataAccessModel.GetRuleSetDef(metadataInitializeData.TenantId);

            Assert.AreEqual("ruleSetXamlUpdated", xaml);
        }

        [TestMethod]
        public void ShouldUpdateWorkflowDef()
        {
            WorkflowDto workflowMock = Mocks.MockHelper.GetWorkflowEntityMock();
            WorkflowDto workflowUpdatedMock = Mocks.MockHelper.GetWorkflowUpdatedEntityMock();

            WorkflowDataModel dataAccessModel = new WorkflowDataModel(factory);

            //insert workflowDef.
            Guid workflowId = dataAccessModel.AddWorkflowDef(metadataInitializeData.TenantId, workflowMock.Name, workflowMock.Xoml);

            //update workflowDef.
            dataAccessModel.UpdateWorkflowDef(metadataInitializeData.TenantId, workflowId, workflowUpdatedMock.Name, workflowUpdatedMock.Xoml);

            //get workflowDef Removed.
            Dictionary<Guid, string> result = dataAccessModel.GetWorkflowDefList(metadataInitializeData.TenantId);

            Assert.IsTrue(result.ContainsKey(workflowId));
            //Assert
            Assert.AreEqual(workflowUpdatedMock.Name, result[workflowId]);
            //Assert.AreEqual(workflowUpdatedMock.Xoml, result.Xoml);

            Dictionary<Guid, string> list = dataAccessModel.GetWorkflowDefList(metadataInitializeData.TenantId);

            //delete workflowDef
            foreach (Guid item in list.Keys)
            {
                dataAccessModel.DeleteWorkflowDef(item);
            }
        }

        [TestMethod]
        public void ShouldGetRuleSetDef()
        {
            WorkflowDataModel dataAccessModel = new WorkflowDataModel(factory);

            //insert a ruleSet for this tenant
            dataAccessModel.UpdateRuleSetDef(metadataInitializeData.TenantId, "XAMLTEST");

            //get RuleSet xaml
            string xaml = dataAccessModel.GetRuleSetDef(metadataInitializeData.TenantId);
            Assert.IsNotNull(xaml);
            Assert.AreEqual("XAMLTEST", xaml);
        }
    }
}
